package com.guguskins.back.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.entity.GuAccount;
import com.guguskins.back.entity.GuStock;
import com.guguskins.back.mapper.GuStockMapper;
import com.guguskins.back.service.GuAccountService;
import com.guguskins.back.service.GuStockService;
import com.guguskins.back.utils.steam.SteamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-30
 */
@Service
public class GuStockServiceImpl extends ServiceImpl<GuStockMapper, GuStock> implements GuStockService {

    @Autowired
    private GuStockMapper stockMapper;

    @Autowired
    private GuAccountService accountService;

    @Override
    public boolean getStock(Integer accountId, String accountNumber, String steamId) {
        boolean result = true;

        JSONObject inventory = SteamUtil.getInventory(steamId);
        JSONArray assets = inventory.getJSONArray("assets");
        JSONArray descriptions = inventory.getJSONArray("descriptions");

        List<String> assetIds = new ArrayList<>();

        label1:
        for (int i = 0; i < assets.size(); i++) {
            GuStock stock = new GuStock();
            JSONObject asset = assets.getJSONObject(i);
            Integer appid = asset.getInteger("appid");
            stock.setStockAppId(appid);
            stock.setStockSteamId(steamId);
            String assetid = asset.getString("assetid");
            stock.setStockAssetId(assetid);
            String contextid = asset.getString("contextid");
            stock.setStockContextId(contextid);
            String classid = asset.getString("classid");
            for (int j = 0; j < descriptions.size(); j++) {
                JSONObject description = descriptions.getJSONObject(j);
                List<String> types = Arrays.asList("匕首", "微型冲锋枪", "手套", "手枪", "机枪", "步枪", "武器箱", "狙击步枪", "霰弹枪");
                if (description.getString("classid").equals(classid)) {
                    String type = description.getString("type");
                    if (ObjectUtils.isEmpty(type.split(" ")[1]) || !types.contains(type.split(" ")[1])) {
                        continue label1;
                    }
                    stock.setStockMarketName(description.getString("market_name"));
                    break;
                }
            }
            stock.setStockAccountId(accountId);
            stock.setStockAccountNumber(accountNumber);
            if (this.count(new LambdaQueryWrapper<GuStock>() {
                {
                    eq(GuStock::getStockAssetId, assetid);
                }
            }) > 0) {
                this.update(stock, new LambdaUpdateWrapper<GuStock>() {
                    {
                        eq(GuStock::getStockAssetId, assetid);
                    }
                });
            } else {
                stock.insert();
            }

            assetIds.add(assetid);

        }
        if (!CollectionUtils.isEmpty(assetIds)) {
            result = this.remove(new LambdaQueryWrapper<GuStock>() {
                {
                    eq(GuStock::getStockSteamId, steamId);
                    notIn(GuStock::getStockAssetId, assetIds);
                }
            });
        }
        return result;
    }

    @Override
    public boolean backUpdate() {
        AtomicBoolean result = new AtomicBoolean(false);
        List<GuAccount> accounts = accountService.list();
        if (!CollectionUtils.isEmpty(accounts)) {
            accounts.forEach(account -> {
                result.set(this.getStock(account.getAccountId(), account.getAccountNumber(), account.getAccountSteamId()));
            });
        }
        return result.get();
    }
}
